package service

import (
	"aigen-todo/internal/interfaces"
	"aigen-todo/internal/models"
)

type TaskLogService struct {
	taskLogRepo interfaces.TaskLogRepositoryInterface
	taskRepo    interfaces.TaskRepositoryInterface
}

func NewTaskLogService(taskLogRepo interfaces.TaskLogRepositoryInterface, taskRepo interfaces.TaskRepositoryInterface) *TaskLogService {
	return &TaskLogService{
		taskLogRepo: taskLogRepo,
		taskRepo:    taskRepo,
	}
}

func (s *TaskLogService) CreateTaskLog(taskID uint64, content string) (*models.TaskLog, error) {
	// 检查任务是否存在
	_, err := s.taskRepo.GetByID(taskID)
	if err != nil {
		return nil, err
	}

	// 创建任务日志
	log := &models.TaskLog{
		TaskID:  taskID,
		Content: content,
	}

	err = s.taskLogRepo.Create(log)
	if err != nil {
		return nil, err
	}

	return log, nil
}

func (s *TaskLogService) GetTaskLogByID(id uint64) (*models.TaskLog, error) {
	return s.taskLogRepo.GetByID(id)
}

func (s *TaskLogService) GetTaskLogsByTaskID(taskID uint64) ([]models.TaskLog, error) {
	return s.taskLogRepo.GetByTaskID(taskID)
}

func (s *TaskLogService) UpdateTaskLog(log *models.TaskLog) error {
	return s.taskLogRepo.Update(log)
}

func (s *TaskLogService) DeleteTaskLog(id uint64) error {
	return s.taskLogRepo.Delete(id)
}
